{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn import metrics\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.subplot(3, 2, 1)\n",
    "x1 = np.array([1, 2, 3, 1, 5, 6, 5, 5, 6, 7, 8, 9, 7, 9])\n",
    "x2 = np.array([1, 3, 2, 2, 8, 6, 7, 6, 7, 1, 2, 1, 1, 3])\n",
    "X = np.array(zip(x1, x2)).reshape(len(x1), 2)\n",
    "\n",
    "plt.xlim([0, 10])\n",
    "plt.ylim([0, 10])\n",
    "plt.title('Instances')\n",
    "plt.scatter(x1, x2)\n",
    "colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'b']\n",
    "markers = ['o', 's', 'D', 'v', '^', 'p', '*', '+']\n",
    "tests = [2, 3, 4, 5, 8]\n",
    "subplot_counter = 1\n",
    "for t in tests:\n",
    "    subplot_counter += 1\n",
    "    plt.subplot(3, 2, subplot_counter)\n",
    "    kmeans_model = KMeans(n_clusters=t).fit(X)\n",
    "    for i, l in enumerate(kmeans_model.labels_):\n",
    "        plt.plot(x1[i], x2[i], color=colors[l], marker=markers[l], ls='None')\n",
    "    plt.xlim([0, 10])\n",
    "    plt.ylim([0, 10])\n",
    "    plt.title('K = %s, Silhouette Coefficient = %.03f' % (\n",
    "        t, metrics.silhouette_score(X, kmeans_model.labels_, metric='euclidean')))\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
